package onlinediary.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import onlinediary.entity.Diary;
import onlinediary.entity.Group;
import onlinediary.entity.User;
import onlinediary.service.IDiaryService;
import onlinediary.service.IGroupService;
import onlinediary.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2022-05-03
 */
@CrossOrigin
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private IDiaryService diaryService;

    @Resource
    private IGroupService groupService;

    @PostMapping("/regist")
    public String regist(Model model, User user) {
        String zid = user.getZid();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getZid, zid);
        User one = userService.getOne(queryWrapper);
        if (one != null) {
            model.addAttribute("msg", "注册失败，账号已存在！");
            return "regist";
        }
        boolean save = userService.save(user);
        if (save) {
            model.addAttribute("msg", "注册成功！登录账号");
            return "login";
        } else {
            model.addAttribute("msg", "注册失败！");
            return "regist";
        }
    }

    @PostMapping("/login")
    public String login(Model model, User user, HttpSession session) {
        String zid = user.getZid();
        String password = user.getPassword();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getZid, zid).eq(User::getPassword, password);
        User one = userService.getOne(queryWrapper);
        if (one != null) {
            session.setAttribute("loginUser", one);
            model.addAttribute("msg", "登录成功！");
            return "index";
        } else {
            model.addAttribute("msg", "密码错误！");
            return "login";
        }
    }

    @GetMapping("/loginOut")
    public String loginOut(HttpSession session, Model model) {
        session.removeAttribute("loginUser");
        model.addAttribute("msg", "退出成功，请重新登录！");
        return "login";
    }

    @GetMapping("/userDetail")
    public String userDetail(HttpSession session, Model model) {
        User user = (User) session.getAttribute("loginUser");

        //查询当前用户的日记总个数
        Integer dcount = Math.toIntExact(diaryService.count(
                new QueryWrapper<Diary>()
                        .lambda()
                        .eq(Diary::getUId, user.getId())
        ));
        //查询当前用户的日记分组个数
        Integer gcount = Math.toIntExact(groupService.count(
                new QueryWrapper<Group>()
                        .lambda()
                        .eq(Group::getUId, user.getId())
        ));

        user.setDcount(dcount);
        user.setGcount(gcount);

        model.addAttribute("user", user);
        return "userMessageManage";
    }

    @PostMapping("/updateP")
    public String updateP(String password, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        user.setPassword(password);
        userService.updateById(user);
        return "redirect:/user/userDetail";
    }
}
